<?php


namespace models\Business;


use models\Exception\Business\SettingsException;
use models\Exception\Business\SystemException;
use models\Exception\DAO\ModelDriverException;
use models\Exception\DAO\ModelException;
use models\Exception\DAO\ModelReflectionException;
use models\Exception\DAO\ModelSqlException;
use models\Exception\Service\ServiceException;
use models\Exception\Transformer\TransformerException;
use models\Service\SettingsService;
use models\Service\SystemService;

class System
{
    static public function getSettingsInfo($id = 0) {
        try {
            return SettingsService::getInstance()->getOne($id);
        } catch (ModelDriverException $e) {
            throw $e;
        } catch (ModelException $e) {
            throw $e;
        } catch (ModelReflectionException $e) {
            throw $e;
        } catch (ModelSqlException $e) {
            throw $e;
        } catch (ServiceException $e) {
            throw $e;
        } catch (TransformerException $e) {
            throw $e;
        }
    }


    static public function editSettingsInfo($parameters = []) {
        try {
            if (! isset($parameters['id']) || empty($parameters['id']))
                throw new SystemException(SystemException::SYSTEM_ID_IS_EMPTY, SystemException::SYSTEM_ID_IS_EMPTY_NO);

            $update_arr = [];
            if (isset($parameters['coupon_in_hot_title']))
                $update_arr['coupon_in_hot_title'] = $parameters['coupon_in_hot_title'];
            if (isset($parameters['coupon_list_title']))
                $update_arr['coupon_list_title'] = $parameters['coupon_list_title'];
            if (isset($parameters['coupon_intro_in_details']))
                $update_arr['coupon_intro_in_details'] = $parameters['coupon_intro_in_details'];
            if (isset($parameters['coupon_rebate_in_details']))
                $update_arr['coupon_rebate_in_details'] = $parameters['coupon_rebate_in_details'];
            if (isset($parameters['coupon_wait_for_coupon_list']))
                $update_arr['coupon_wait_for_coupon_list'] = $parameters['coupon_wait_for_coupon_list'];
            if (isset($parameters['coupon_wait_for_coupon_title']))
                $update_arr['coupon_wait_for_coupon_title'] = $parameters['coupon_wait_for_coupon_title'];
            if (isset($parameters['coupon_button']))
                $update_arr['coupon_button'] = $parameters['coupon_button'];

            if (!empty($update_arr))
                $system = SettingsService::getInstance()->update($update_arr, $parameters['id']);
            else
                $system = SettingsService::getInstance()->getOne($parameters['id']);

            return $system;
        } catch (SystemException $e) {
            throw $e;
        } catch (ModelDriverException $e) {
            if ($e->getCode() == ModelDriverException::DRIVER_EXCEPTION_MSG_NO)
                throw new SettingsException(SettingsException::SETTINGS_EDIT_FAILURE, SettingsException::SETTINGS_EDIT_FAILURE_NO);
            throw $e;
        } catch (ModelException $e) {
            throw $e;
        } catch (ModelReflectionException $e) {
            throw $e;
        } catch (ModelSqlException $e) {
            throw $e;
        } catch (ServiceException $e) {
            throw $e;
        } catch (TransformerException $e) {
            throw $e;
        }

    }

    static public function getSystemInfo($id = 0) {
        try {
            return SystemService::getInstance()->getOne($id);
        } catch (ModelDriverException $e) {
            throw $e;
        } catch (ModelException $e) {
            throw $e;
        } catch (ModelReflectionException $e) {
            throw $e;
        } catch (ModelSqlException $e) {
            throw $e;
        } catch (ServiceException $e) {
            throw $e;
        } catch (TransformerException $e) {
            throw $e;
        }
    }

    static public function editSystemInfo($parameters = []) {
        try {
            if (! isset($parameters['id']) || empty($parameters['id']))
                throw new SystemException(SystemException::SYSTEM_ID_IS_EMPTY, SystemException::SYSTEM_ID_IS_EMPTY_NO);

            $update_arr = [];
            if (isset($parameters['default_post_fee']))
                $update_arr['default_post_fee'] = $parameters['default_post_fee'];
            if (isset($parameters['exchange_rate']))
                $update_arr['exchange_rate'] = $parameters['exchange_rate'];
            if (isset($parameters['coupon_expire']))
                $update_arr['coupon_expire'] = $parameters['coupon_expire'];
            if (isset($parameters['coupon_bot_rate']))
                $update_arr['coupon_bot_rate'] = $parameters['coupon_bot_rate'];
            if (isset($parameters['alibaba_price']))
                $update_arr['alibaba_price'] = $parameters['alibaba_price'];
            if (isset($parameters['rebate_rate']))
                $update_arr['rebate_rate'] = $parameters['rebate_rate'];
            if (isset($parameters['title']))
                $update_arr['title'] = $parameters['title'];
            if (isset($parameters['title_suffix']))
                $update_arr['title_suffix'] = $parameters['title_suffix'];
            if (isset($parameters['keywords']))
                $update_arr['keywords'] = $parameters['keywords'];
            if (isset($parameters['description']))
                $update_arr['description'] = $parameters['description'];
            if (isset($parameters['price_score_rules']))
                $update_arr['price_score_rules'] = $parameters['price_score_rules'] == 0 ? 0 : json_encode($parameters['price_score_rules']);
            if (isset($parameters['sales_score_rules']))
                $update_arr['sales_score_rules'] = $parameters['sales_score_rules'] == 0 ? 0 : json_encode($parameters['sales_score_rules']);
            if (isset($parameters['sales_in_30_days_score_rules']))
                $update_arr['sales_in_30_days_score_rules'] = $parameters['sales_in_30_days_score_rules'] == 0 ? 0 : json_encode($parameters['sales_in_30_days_score_rules']);
            if (isset($parameters['favour_score_rules']))
                $update_arr['favour_score_rules'] = $parameters['favour_score_rules'] == 0 ? 0 : json_encode($parameters['favour_score_rules']);
            if (isset($parameters['click_score_rules']))
                $update_arr['click_score_rules'] = $parameters['click_score_rules'] == 0 ? 0 : json_encode($parameters['click_score_rules']);
            if (isset($parameters['order_score_rules']))
                $update_arr['order_score_rules'] = $parameters['order_score_rules'] == 0 ? 0 : json_encode($parameters['order_score_rules']);
            if (isset($parameters['download_iOS']))
                $update_arr['download_iOS'] = $parameters['download_iOS'];
            if (isset($parameters['download_Android']))
                $update_arr['download_Android'] = $parameters['download_Android'];
            if (isset($parameters['about_us']))
                $update_arr['about_us'] = $parameters['about_us'];
            if (isset($parameters['coupon_protocol']))
                $update_arr['coupon_protocol'] = $parameters['coupon_protocol'];
            if (isset($parameters['refund_protocol']))
                $update_arr['refund_protocol'] = $parameters['refund_protocol'];
            if (isset($parameters['return_protocol']))
                $update_arr['return_protocol'] = $parameters['return_protocol'];
            if (isset($parameters['customer_service']))
                $update_arr['customer_service'] = $parameters['customer_service'];
            if (isset($parameters['customer_qq']))
                $update_arr['customer_qq'] = $parameters['customer_qq'];
            if (isset($parameters['customer_wechat']))
                $update_arr['customer_wechat'] = $parameters['customer_wechat'];
            if (isset($parameters['advance']))
                $update_arr['advance'] = $parameters['advance'];
            if (isset($parameters['help']))
                $update_arr['help'] = $parameters['help'];
            if (isset($parameters['record']))
                $update_arr['record'] = $parameters['record'];
            if (isset($parameters['order_info']))
                $update_arr['order_info'] = $parameters['order_info'];
            if (isset($parameters['tbk_rebate_rate']))
                $update_arr['tbk_rebate_rate'] = $parameters['tbk_rebate_rate'];
            if (isset($parameters['tbk_parent_rate']))
                $update_arr['tbk_parent_rate'] = $parameters['tbk_parent_rate'];
            if (isset($parameters['tbk_sub_rate']))
                $update_arr['tbk_sub_rate'] = $parameters['tbk_sub_rate'];
            if (isset($parameters['parent_rate']))
                $update_arr['parent_rate'] = $parameters['parent_rate'];
            if (isset($parameters['sub_rate']))
                $update_arr['sub_rate'] = $parameters['sub_rate'];
            if (isset($parameters['service_rate']))
                $update_arr['service_rate'] = $parameters['service_rate'];
            if (isset($parameters['registration_agreement']))
                $update_arr['registration_agreement'] = $parameters['registration_agreement'];
            if (isset($parameters['cmc_terms_agreement']))
                $update_arr['cmc_terms_agreement'] = $parameters['cmc_terms_agreement'];
            if (isset($parameters['coupon_introduction']))
                $update_arr['coupon_introduction'] = $parameters['coupon_introduction'];
            if (isset($parameters['integral_introduction']))
                $update_arr['integral_introduction'] = $parameters['integral_introduction'];
            if (isset($parameters['legal_notice']))
                $update_arr['legal_notice'] = $parameters['legal_notice'];
            if (isset($parameters['privacy_agreement']))
                $update_arr['privacy_agreement'] = $parameters['privacy_agreement'];
            if (isset($parameters['coupon_delay']))
                $update_arr['coupon_delay'] = $parameters['coupon_delay'];
            if (isset($parameters['userwin']))
                $update_arr['userwin'] = $parameters['userwin'];
            if (isset($parameters['userback']))
                $update_arr['userback'] = $parameters['userback'];

            if (!empty($update_arr))
                $system = SystemService::getInstance()->update($update_arr, $parameters['id']);

            else
                $system = SystemService::getInstance()->getOne($parameters['id']);

            return $system;
        } catch (SystemException $e) {
            throw $e;
        } catch (ModelDriverException $e) {
            if ($e->getCode() == ModelDriverException::DRIVER_EXCEPTION_MSG_NO)
                throw new SystemException(SystemException::SYSTEM_EDIT_FAILURE, SystemException::SYSTEM_EDIT_FAILURE_NO);
            throw $e;
        } catch (ModelException $e) {
            throw $e;
        } catch (ModelReflectionException $e) {
            throw $e;
        } catch (ModelSqlException $e) {
            throw $e;
        } catch (ServiceException $e) {
            throw $e;
        } catch (TransformerException $e) {
            throw $e;
        }

    }

    public function getScoreRules() {
        try {
            /*$price_score_arr = [
                [
                    'price' => 10,
                    'number' => 5,
                    'coefficient' => 1
                ],
                [
                    'price' => 40,
                    'number' => 10,
                    'coefficient' => 1
                ],
                [
                    'price' => 60,
                    'number' => 8,
                    'coefficient' => 1
                ],
                [
                    'price' => 100,
                    'number' => 4,
                    'coefficient' => 1
                ],
                [
                    'price' => 300,
                    'number' => 2,
                    'coefficient' => 1
                ],
            ];
            // if(`platform_sales` > sales, number * coefficient, 0)
            $sales_score_arr = [
                [
                    // if中用于判断的数值
                    'sales' => 0,
                    // if判断为true时的值，若为0，则为`platform_sales`字段
                    'number' => 0,
                    // number所乘的系数
                    'coefficient' => 0.1
//            ],
//            [
//                'sales' => 50,
//                'number' => 0,
//                'coefficient' => 0.2
                ]
            ];
            $sales_in_30_days_score = [
                [
                    // if中用于判断的数值
                    'sales_in_30_days' => 0,
                    // if判断为true时的值，若为0，则为`sales_in_30_days`字段
                    'number' => 0,
                    // number所乘的系数
                    'coefficient' => 0.1
//            ],
//            [
//                'sales' => 50,
//                'number' => 0,
//                'coefficient' => 0.02
                ]
            ];
            $default_sales_in_30_days_score = 0;
            $favours_score_arr = [
                [
                    // if中用于判断的数值
                    'favours' => 0,
                    // if判断为true时的值，若为0，则为`platform_sales`字段
                    'number' => 0,
                    // number所乘的系数
                    'coefficient' => 0.1
//            ],
//            [
//                'favours' => 50,
//                'number' => 0,
//                'coefficient' => 0.2
                ]
            ];
            $view_click_conversion_score_arr = [
                [
                    // if中用于判断的数值
                    'clicks/views' => 0,
                    // if判断为true时的值，若为0，则为`platform_sales`字段
                    'number' => 0,
                    // number所乘的系数
                    'coefficient' => 200
//            ],
//            [
//                'favours' => 50,
//                'number' => 0,
//                'coefficient' => 0.2
                ]
            ];
            $click_order_conversion_score_arr = [
                [
                    // if中用于判断的数值
                    'platform_sales/clicks' => 0,
                    // if判断为true时的值，若为0，则为`platform_sales`字段
                    'number' => 0,
                    // number所乘的系数
                    'coefficient' => 200
//            ],
//            [
//                'favours' => 50,
//                'number' => 0,
//                'coefficient' => 0.2
                ]
            ];*/
            $system = SystemService::getInstance()->getOne(1);
            $price_score_arr = json_decode($system['price_score_rules'], true);
            $sales_score_arr = json_decode($system['sales_score_rules'], true);
            $sales_in_30_days_score = json_decode($system['sales_in_30_days_score_rules'], true);
            $favour_score_arr = json_decode($system['favour_score_rules'], true);
            $view_click_conversion_score_arr = json_decode($system['click_score_rules'], true);
            $click_order_conversion_score_arr = json_decode($system['order_score_rules'], true);

            return [$price_score_arr, $sales_score_arr, $sales_in_30_days_score, $favour_score_arr, $view_click_conversion_score_arr, $click_order_conversion_score_arr];
        } catch (ModelDriverException $e) {
            throw $e;
        } catch (ModelException $e) {
            throw $e;
        } catch (ModelReflectionException $e) {
            throw $e;
        } catch (ModelSqlException $e) {
            throw $e;
        } catch (ServiceException $e) {
            throw $e;
        } catch (TransformerException $e) {
            throw $e;
        }

    }

}
